#!/usr/bin/perl

#------------------------------------------------------------------------------
# Author:  Marelie Davel (marelie.davel@gmail.com)
#------------------------------------------------------------------------------

use netDraw;
use strict;
use warnings;
$netDraw::debug=0;

#------------------------------------------------------------------------------

sub print_usage() {
  print "Usage: draw_class.pl <in:classes> <in:counts> <in:specs> <out:prefix>\n";
  print "       <in:classes>   = Rule classes to use in tab-delimited 2-column format\n";
  print "       <in:counts>    = Instances in tab-delimited 3-column format\n";
  print "       <in:specs>     = Option specifications file\n";
  print "       <out:prefix>   = Prefix used for output .dot and image file\n";
}

#------------------------------------------------------------------------------

sub read_specs($$) {
  my ($inName,$extP) = @_;
  open my $in, '<:encoding(utf8)', $inName or die "Cannot open $inName";
  my %specs;
  while (<$in>) {
    chomp;
    next if /^\s*#/;
    next if /^\s*$/;
    $_ =~ s/#.*$//;
    $_ =~ s/\s*$//;
    my @parts = split /\s+/;
    if (scalar @parts != 2) {
      die "Error: wrong format file $inName line $_";
    }
    $specs{$parts[0]} = $parts[1];
  }
  $$extP = "jpeg";
  foreach my $v (keys %specs) {
    if ($v =~  /draw_class_ext/) {
      $$extP = $specs{$v};
    }
  } 
}

#------------------------------------------------------------------------------

sub draw_classes($$$$) {
  my ($classes, $counts, $specsName, $prefix) = @_;
  my $ext;
  read_specs($specsName,\$ext);
  fdraw_class_net($classes,$counts,$ext,$prefix);
}

if (scalar @ARGV == 4) {
  draw_classes($ARGV[0],$ARGV[1],$ARGV[2],$ARGV[3]);
} else {
  print_usage();
}

#------------------------------------------------------------------------------
